<template>
  <div>
    <!-- 关闭按钮 -->
    <van-nav-bar title="我的关注" left-arrow @click-left="onClickLeft" />
    <div class="item" :key="item.id" v-for="(item,index) in userList">
      <div class="left">
        <van-image round v-if="item.head_img" :src="$http.defaults.baseURL+item.head_img" />
        <van-image round v-else :src="require('../assets/logo.jpg')" />
        <div class="info">
          <p>{{item.nickname}}</p>
          <span>{{item.create_date.split('T')[0]}}</span>
        </div>
      </div>
      <!-- 取消关注 -->
      <div v-if="item.focus" class="right">
        <van-button
          color="#b3b3b3"
          round
          size="small"
          type="info"
          @click="delfocus(item.id,index)"
        >已关注</van-button>
      </div>
      <!-- 关注 -->
      <div v-else class="right focus">
        <van-button color="#b3b3b3" round size="small" @click="addfocus(item.id,index)">关注</van-button>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      userList: []
    }
  },
  created() {
    this.getuserList()
  },
  methods: {
    //取消关注
    async unFocus(id) {},
    onClickLeft() {
      this.$router.go(-1)
    },
    //获取关注的用户
    async getuserList() {
      const { data: res } = await this.$http.get('/user_follows')
      const newData = res.data.map(item => {
        return {
          ...item,
          focus: true
        }
      })

      this.userList = newData
      console.log(this.userList)
    },
    //取消关注
    async delfocus(id, i) {
      const { data: res } = await this.$http.get('/user_unfollow/' + id)
      console.log(res)
      if (res.message === '取消关注成功') {
        this.userList[i].focus = false
      }
    },
    // 关注
    async addfocus(id, i) {
      const {data:res} = await this.$http.get('/user_follows/'+id)
      if(res.message === '关注成功'){
        this.userList[i].focus = true
      }
    }
  }
}
</script>
<style lang="less" scoped>
.item {
  height: 25vw;
  padding: 0 5.556vw;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 0.278vw solid #eee;
  .left {
    height: 16.667vw;
    display: flex;
    .van-image {
      width: 13.889vw;
      height: 13.889vw;
      align-self: center;
      margin-right: 4.167vw;
    }
    .info {
      height: 100%;
      display: flex;
      justify-content: center;
      flex-direction: column;
      span {
        color: #ccc;
        font-size: 3.889vw;
        line-height: 5.556vw;
      }
    }
  }
}
.right {
  float: right;
}
.focus {
  .van-button {
    background: red !important;
    border: none !important;
  }
}
</style>